import { Application } from 'egg';
import * as pageFindPlugin from 'mongoose-paginate';
import { GenderType, MemberType, GenderNameList } from '../constants';
import * as moment from 'moment';
import isPositiveInt from '../validator';

export default (app: Application) => {
  const mongoose = app.mongoose;
  const schema = new mongoose.Schema({
    ctime: {
      type: Date,
      default: Date.now,
      required: true,
    },
    type: {
      type: String,
      required: true,
      default: 'express',
    },
    name: {
      type: String,
      required: true,
    },
    mobile: {
      type: String,
      max: 11,
      min: 11,
      required: false,
    },
    realName: {
      type: String,
      required: false,
    },
    id: {
      type: String,
      required: false,
    },
    // 身份证url
    cardUrl: {
      type: String,
      required: false,
    },
    addressProvince: {
      type: String,
      required: false,
    },
    addressCity: {
      type: String,
      required: false,
    },
    addressCounty: {
      type: String,
      required: false,
    },
    addressDetail: {
      type: String,
      required: false,
    },
    openId: {
      type: String,
      required: true,
    },
    site: {
      type: String,
      required: false,
    },
    unionId: {
      type: String,
      required: true,
    },
    avatarUrl: {
      type: String,
      required: false,
    },
    status: {
      type: String,
      default: 0, // 默认为0,1为暂停使用,3为删除
    },
    // 友帮币
    coin: {
      type: Number,
      required: true,
      default: 0,
    },
    // 性别
    gender: {
      type: Number,
      required: false,
      enum: [ GenderType.female, GenderType.male ],
    },
    // 生日
    birthday: {
      type: Date,
      required: false,
    },
    // 余额(单位分)
    balance: {
      type: Number,
      default: 0,
      required: true,
      validate: isPositiveInt,
    },
    // 年费
    deposit: {
      type: Number,
      required: true,
      default: 0,
      validate: isPositiveInt,
    },
    // 会员类型
    memberType: {
      type: Number,
      required: true,
      default: MemberType.normal,
      enum: [ MemberType.reCharge, MemberType.deposit, MemberType.normal ],
    },
    // 会员创建时间
    memberCreateAt: {
      type: Date,
      required: false,
    },
    // 年会会员过期时间
    memberExpires: {
      type: Date,
      required: false,
    },
    // 角色
    roles: [
      {
        type: Object,
        ref: 'Role',
      },
    ],
    // 邻里圈背景
    neighbourhoodBgImg: {
      type: String,
      required: false,
    },
  }, { timestamps: true, toJSON: { virtuals: true, getters: true } });

  // 年龄
  // tslint:disable-next-line:ter-prefer-arrow-callback
  schema.virtual('age').get(function (this: any) {
    if (!this.birthday) {
      return null;
    }
    return moment(this.birthday, 'YYYY-MM-DD').fromNow();
  });

  // 性别
  // tslint:disable-next-line:ter-prefer-arrow-callback
  schema.virtual('genderName').get(function (this: any) {
    return GenderNameList[this.gender];
  });

  // 年会会员状态
  // tslint:disable-next-line:ter-prefer-arrow-callback
  schema.virtual('memberStatus').get(function (this: any) {
    return moment().isBefore(this.memberExpires);
  });

  schema.index({
    unionId: 1,
    ctime: -1,
    name: 1,
    site: 1,
    status: 1,
  });

  schema.plugin(pageFindPlugin, {});
  return mongoose.model('User', schema, 'express_user');
};
